﻿using BaseData.API.Read.Applications.Commands.AppoinCommand;
using BaseData.API.Read.Dtos;
using BaseData.Domain.Appointment;
using BaseData.Domain.HospitalInformation;
using BaseData.Domain.MCI;
using BaseData.ErrorCode;
using BaseData.Infrastructure.Interfaces;
using MediatR;

namespace BaseData.API.Read.Applications.CommandHandlers.AppointCommandHandler
{
    /// <summary>
    /// 预约列表命令处理
    /// </summary>
    public class AppointShowCommandHandler : IRequestHandler<AppointShowCommand, ApiResult<ApiPaging<AppointDTO>>>
    {
        /// <summary>
        /// 预约
        /// </summary>
        public IBaseRepository<Appointments> appointRep;
        /// <summary>
        /// 挂号类型
        /// </summary>
        public IBaseRepository<Register> registerRep;
        /// <summary>
        /// 患者
        /// </summary>
        public IBaseRepository<Patient> patientRep;
        /// <summary>
        /// 科室
        /// </summary>
        public IBaseRepository<Departments> deparRep ;
        public AppointShowCommandHandler(IBaseRepository<Appointments> appointRep, IBaseRepository<Register> registerRep, IBaseRepository<Patient> patientRep, IBaseRepository<Departments> deparRep)
        {
            this.appointRep = appointRep;
            this.registerRep = registerRep;
            this.patientRep = patientRep;
            this.deparRep = deparRep;
        }
        //测试
        public Task<ApiResult<ApiPaging<AppointDTO>>> Handle(AppointShowCommand request, CancellationToken cancellationToken)
        {
            ApiResult<ApiPaging<AppointDTO>> result = new ApiResult<ApiPaging<AppointDTO>>();
            result.Code = ApiEnum.查询成功;
            result.Message = ApiEnum.查询成功.ToString();

            var list = from a in appointRep.GetAll()
                       join b in registerRep.GetAll() on a.RegistTypeId equals b.RegisterId
                       join c in patientRep.GetAll() on a.PatientId equals c.PatientId
                       join d in deparRep.GetAll() on a.DepartmentId equals d.DepartmentId
                       select new AppointDTO()
                       {
                           AppointmentId = a.AppointmentId,
                           Attending = a.Attending,
                           DoctorAddress = a.DoctorAddress,
                           Department = a.Department,
                           AttendingName = a.AttendingName,
                           DoctorName = a.DoctorName,
                           RegistTypeId = a.RegistTypeId,
                           OutpatientTime = a.OutpatientTime,
                           WaitingTime = a.WaitingTime,
                           Cost = a.Cost,
                           AppointmentTime = a.AppointmentTime,
                           PatientId = a.PatientId,
                           MedicalState = a.MedicalState,
                           Reason = a.Reason,
                           CancelTime = a.CancelTime,
                           Desc = a.Desc,
                           RegisterName = b.RegisterName,
                           Relationship=c.Relationship,
                           Name=c.Name,
                           IdCardType=c.IdCardType,
                           IdCardNum=c.IdCardNum,
                           MedicalCard=c.MedicalCard,
                           Phone=c.Phone,
                           DepartmentId=a.DepartmentId,
                           DepartmentName=d.DepartmentName,
                       };
            if (request.AppointmentTime!=null)
            {
                list=list.Where(s=>s.AppointmentTime==request.AppointmentTime);
            }

            if (!string.IsNullOrEmpty(request.Name))
            {
                list = list.Where(s => s.Name.Contains(request.Name));
            }
            if (request.DepartmentId != null)
            {
                list = list.Where(x => x.DepartmentId == (request.DepartmentId));
            }
        
            int totalCount = list.Count();
            int pageCount = (int)Math.Ceiling(totalCount * 1.0 / request.pageSize);
            list = list.Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize);

            ApiPaging<AppointDTO> page = new ApiPaging<AppointDTO>();
            page.PageCount = pageCount;
            page.TotalCount = totalCount;
            page.Data = list.ToList();

            result.Data = page;

            return Task.FromResult(result);
        }
    }
}
